package pro.reznick.geoalarm.map;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.*;
import android.widget.RelativeLayout;
import org.osmdroid.DefaultResourceProxyImpl;
import org.osmdroid.ResourceProxy;
import org.osmdroid.api.IGeoPoint;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.OverlayItem;
import pro.reznick.geoalarm.AlarmData;
import pro.reznick.geoalarm.GeoAlarmBaseActivity;
import pro.reznick.geoalarm.R;


public class GetCoordsActivity extends GeoAlarmBaseActivity
{

    private static final String PREFERENCES_KEY = "pro.reznick.geoalarm.PREFERENCES_KEY";
    private static final String ZOOM_LEVEL = "pro.reznick.geoalarm.ZOOM_LEVEL";
    private static final String SCROLL_X = "pro.reznick.geoalarm.SCROLL_X";
    private static final String SCROLL_Y = "pro.reznick.geoalarm.SCROLL_Y";

    private SharedPreferences preferences;
    private MapView mapView;
    private AlarmOverlay overlay;
    private ResourceProxy resourceProxy;


    private double longitude;
    private double lattitude;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void onControllerBoundEvent()
    {
        resourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
        preferences = getSharedPreferences(PREFERENCES_KEY, MODE_PRIVATE);

        final RelativeLayout rl = new RelativeLayout(this);
        this.mapView = new MapView(this, 256, resourceProxy);

        this.overlay = new AlarmOverlay(this.getResources().getDrawable(R.drawable.marker),
                resourceProxy);

        for (AlarmData alarm : controllerTrait.controller.getAlarms())
        {
            OverlayItem item = new OverlayItem(alarm.getName(), alarm.getText(), new GeoPoint(alarm.getLatitude(), alarm.getLongitude()));
            item.setMarkerHotspot(OverlayItem.HotspotPlace.CENTER);
            overlay.addOverlay(item);
        }

        this.mapView.setBuiltInZoomControls(true);
        this.mapView.setMultiTouchControls(true);
        this.mapView.getOverlays().add(this.overlay);
        rl.addView(this.mapView, new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
                RelativeLayout.LayoutParams.FILL_PARENT));

        this.setContentView(rl);

        mapView.getController().setZoom(preferences.getInt(ZOOM_LEVEL, 1));

        boolean setCenter = false;

        if (!setCenter)
            mapView.scrollTo(preferences.getInt(SCROLL_X, 0), preferences.getInt(SCROLL_Y, 0));
        registerForContextMenu(mapView);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
                                    ContextMenu.ContextMenuInfo menuInfo)
    {

        if (lattitude == 0.0 && longitude == 0.0)
            return;

        super.onCreateContextMenu(menu, v, menuInfo);

        if (v == mapView)
        {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.get_coordinates, menu);
            menu.setHeaderTitle(String.format("%5f,%5f", lattitude, longitude));
        }
    }

    @Override
    public boolean onContextItemSelected(MenuItem item)
    {

        if (lattitude == 0.0 && longitude == 0.0)
            return true;

        switch (item.getItemId())
        {
            case R.id.set_coordinate:
                Intent i = new Intent();
                i.putExtra(MapConstants.LONGITUDE_EXTRA, longitude);
                i.putExtra(MapConstants.LATITUDE_EXTRA, lattitude);
                setResult(RESULT_OK, i);
                finish();
                break;
            case R.id.cancel:
                return true;
            default:
                break;

        }
        return true;
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev)
    {
        int action = ev.getAction();
        if (action == MotionEvent.ACTION_DOWN)
        {
            MapView.Projection proj = mapView.getProjection();
            IGeoPoint loc = proj.fromPixels((int) ev.getX(), (int) ev.getY());
            longitude = loc.getLongitudeE6() / 1000000.0;
            lattitude = loc.getLatitudeE6() / 1000000.0;


        }
        return super.dispatchTouchEvent(ev);    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    protected void onPause()
    {
        final SharedPreferences.Editor edit = preferences.edit();
//		edit.putString(PREFS_TILE_SOURCE, mOsmv.getTileProvider().getTileSource().name());
        edit.putInt(SCROLL_X, mapView.getScrollX());
        edit.putInt(SCROLL_Y, mapView.getScrollY());
        edit.putInt(ZOOM_LEVEL, mapView.getZoomLevel());
//		edit.putBoolean(PREFS_SHOW_LOCATION, mLocationOverlay.isMyLocationEnabled());
//		edit.putBoolean(PREFS_SHOW_COMPASS, mLocationOverlay.isCompassEnabled());
        edit.commit();

        super.onPause();
    }


}
